package com.sxrong.carloan.activity;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.sxrong.carloan.R;
import com.sxrong.carloan.dto.LoginBean;
import com.sxrong.carloan.net.ApiService;
import com.sxrong.carloan.net.MyCallBack;
import com.sxrong.carloan.net.XUtils;
import com.sxrong.carloan.utils.AppCacheSharedPreferences;
import com.sxrong.carloan.utils.Config;

import java.util.HashMap;


public class LoginActivity extends Activity implements View.OnClickListener{
    private EditText phone;
    private EditText password;
    private Button button;

    @SuppressLint("WrongViewCast")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        phone = findViewById(R.id.phone);
        password = findViewById(R.id.password);
        button = findViewById(R.id.login_btn);

        button.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.login_btn:
                login();
                break;
        }
    }

    public void login(){
        HashMap<String ,String> hashMap = new HashMap<>();
        hashMap.put("LoginName" ,phone.getText().toString().trim());
        hashMap.put("Password" ,password.getText().toString().trim());
        XUtils.Get(ApiService.LOGIN_URL , hashMap , new MyCallBack<String>(){

            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                if(isFinishing()){
                    return;
                }

                LoginBean loginBean = new Gson().fromJson(result , LoginBean.class);
                if(loginBean.getCode() == 1){
                    AppCacheSharedPreferences.putCacheString(Config.PARTNER_ID, loginBean.getData().getId() + "");
                    AppCacheSharedPreferences.putCacheBoolean(Config.IS_LOGIN , true);
                    setResult(RESULT_OK);
                    finish();
                }else{
                    Toast.makeText(LoginActivity.this, loginBean.getMsg() , Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                if(isFinishing()){
                    return;
                }
                Log.d("LoginActivity" , ex.getMessage());
                Toast.makeText(LoginActivity.this, "请检查您的网络连接", Toast.LENGTH_SHORT).show();
            }
        });


    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        setResult(RESULT_CANCELED);
    }
}
